** Data reading and variable selection from raw data
** Taiwan Social Change Survey 2001


** 01. Reading data **
cap log close
clear all
set more off
cd /*insert you work directory here*/
unicode encoding set Big5 
unicode translate Family.dta,invalid
use Family.dta
save Family.dta, replace
numlabel, add

cd /*insert you work directory here*/


** 02. Consructing year and country variables **

lab var year "survey year"

ge country=158
lab var country "ISO country code"
//Taiwan: 158 (see "ISO Country Codes.pdf) 


** 03. ID variables **

ge pid=id
lab var pid "person id"


** 04. Basic Demographics (Sex and Age/birth year) **

ge sex=a1
lab var sex "sex"
lab def sex 1 "male" 2 "female"
lab val sex sex

lab var age "age"

ge birthyr=1995-age
lab var birthyr "year of birth"


** 05. Siblings **

//married brothers + unmarried brothers
ge nbro=d6a+d6b if d6a<=13 & d6b<=7
replace nbro=d6a if d6a<=13 & d6b>7
replace nbro=d6b if d6a>13 & d6b<=7
replace nbro=0 if d6a>13 & d6b>7
lab var nbro "number of brothers"

//married sisters + unmarried sisters
ge nsis=d6c+d6d if d6c<=13 & d6d<=7
replace nsis=d6c if d6c<=13 & d6d>7
replace nsis=d6d if d6c>13 & d6d<=7
replace nsis=0 if d6c>13 & d6d>7
lab var nsis "number of sisters"

//number of siblings
ge nsibs=nbro+nsis
lab var nsibs "number of siblings"
//birth order not available


** 06. Own education **

//highest level of education obtained
ge educ=b1
lab var educ "highest education attained"

lab def educ 1 "no education" 2 "self-taught" 3 "primary school" 4 "junior school_normal" 5 "junior school_technical school" ///
6 "high school_normal" 7 "high school_technical subjects" 8 "high school_technical school" 9 "noncommissioned school" ///
10 "technical college_5 year" 11 "technical college_2 year" 12 "technical college_3 year" 13 "military and police training classes" ///
14 "Military and police specialist class" 15 "online_technical college" 16 "army police school" 17 "university" ///
18 "postgraduate" 19 "other" 97 "cannot remember"

lab val educ educ


** 07. Parents' education: Father and/or Mother **

//highest education obtained
ge faeduc=b2
lab var faeduc "father highest education obtained"

ge moeduc=b3
lab var moeduc "mother highest education obtained"

lab val faeduc moeduc educ


** 08. Own occupation **

//employment status
ge empstat=c3e
lab var empstat "employment status"

lab def empstat 1 "self-employed/no other employee" 2 "self-employed/have other employees" 3 "work for private employers/organisations" ///
4 "work for public sectors" 5 "work for government sectors" 6 "work for family with regular pay" 7 "work for family with no pay" ///
8 "work for no fixed employer" 9 "housework" 96 "no answer" 97 "don't know" 98 "refuse to anwer" 99 "not applicable"

lab val empstat empstat

//industry
rename c3a ind
lab var ind "industry"

//occupation
rename c3b occ
lab var occ "occupation"


** 09. Parents' occupation **

//mother occupation not available
//code reference all in Chinese
//fathers job when respondent 18
rename c1a faind18 
lab var faind18 "father's industry when respondent 18"
rename c1b faocc18
lab var faocc18 "father's occupation when respondent 18"

//father current occupation
rename c2a faind 
lab var faind "father's current industry (or industry before retirement)"
rename c2b faocc
lab var faocc "father's current occupation (or industry before retirement)"


** 10. Tabulate the Identified Variables **

log using /*insert you work directory here*/, replace text

** Data reading and variable selection from raw data
** Taiwan social change survey 2001

** Sex **
tab sex

** Age, Birth Year **
sum age birthyr, d

** Siblings **
sum nsibs nbro nsis, d

** R's Own Education **
tab1 educ 

** Parental Education **
tab1 faeduc moeduc 

** R's Own Occupation **
tab1 empstat ind occ

** Parental Occupation **
tab1 faind18 faocc18 faind faocc


log close

** 11. Keep the identified variables only

keep year country pid sex age birthyr ///
	 nbro nsis nsibs ///
	 educ faeduc moeduc ///
	 empstat ind occ ///
	 faind18 faocc18 faind faocc


** 12. Save the Data File **

saveold /*insert you work directory here*/, replace



** 13. Homoginising education (by Manting Chen, 2017/4/3)**
** Own Education **
rename educ educ_cat

ge educ_yrs=0 if educ_cat==1
replace educ_yrs=0 if educ_cat==2
replace educ_yrs=6 if educ_cat==3
replace educ_yrs=9 if educ_cat==4
replace educ_yrs=9 if educ_cat==5
replace educ_yrs=12 if educ_cat==6
replace educ_yrs=12 if educ_cat==7
replace educ_yrs=12 if educ_cat==8
replace educ_yrs=14 if educ_cat==9
replace educ_yrs=14 if educ_cat==10
replace educ_yrs=15 if educ_cat==11
replace educ_yrs=14 if educ_cat==12
replace educ_yrs=14 if educ_cat==13
replace educ_yrs=14 if educ_cat==14
replace educ_yrs=14 if educ_cat==15
replace educ_yrs=15 if educ_cat==16
replace educ_yrs=16 if educ_cat==17
replace educ_yrs=19 if educ_cat==18
replace educ_yrs=. if educ_cat==19
lab var educ_yrs "respondent highest education in years"

ge educ_ISCED=020 if educ_cat==1
replace educ_ISCED=020 if educ_cat==2
replace educ_ISCED=100 if educ_cat==3
replace educ_ISCED=200 if educ_cat==4
replace educ_ISCED=200 if educ_cat==5
replace educ_ISCED=340 if educ_cat==6
replace educ_ISCED=350 if educ_cat==7
replace educ_ISCED=350 if educ_cat==8
replace educ_ISCED=300 if educ_cat==9
replace educ_ISCED=500 if educ_cat==10
replace educ_ISCED=500 if educ_cat==11
replace educ_ISCED=500 if educ_cat==12
replace educ_ISCED=500 if educ_cat==13
replace educ_ISCED=500 if educ_cat==14
replace educ_ISCED=500 if educ_cat==15
replace educ_ISCED=500 if educ_cat==16
replace educ_ISCED=600 if educ_cat==17
replace educ_ISCED=700 if educ_cat==18
replace educ_ISCED=. if educ_cat==19
lab var educ_ISCED "respondent highest education in years"


** Parents Education **
//father's education is actually father's
ge faeduc_flag=1 

rename faeduc faeduc_cat
rename moeduc maeduc_cat

ge faeduc_yrs=0 if faeduc_cat==1
replace faeduc_yrs=0 if faeduc_cat==2
replace faeduc_yrs=6 if faeduc_cat==3
replace faeduc_yrs=9 if faeduc_cat==4
replace faeduc_yrs=9 if faeduc_cat==5
replace faeduc_yrs=12 if faeduc_cat==6
replace faeduc_yrs=12 if faeduc_cat==7
replace faeduc_yrs=12 if faeduc_cat==8
replace faeduc_yrs=14 if faeduc_cat==9
replace faeduc_yrs=14 if faeduc_cat==10
replace faeduc_yrs=15 if faeduc_cat==11
replace faeduc_yrs=14 if faeduc_cat==12
replace faeduc_yrs=14 if faeduc_cat==13
replace faeduc_yrs=14 if faeduc_cat==14
replace faeduc_yrs=14 if faeduc_cat==15
replace faeduc_yrs=15 if faeduc_cat==16
replace faeduc_yrs=16 if faeduc_cat==17
replace faeduc_yrs=19 if faeduc_cat==18
replace faeduc_yrs=. if faeduc_cat==19 | faeduc_cat==96 | faeduc_cat==97 | faeduc_cat==98
lab var faeduc_yrs "father's education in years"

ge maeduc_yrs=0 if maeduc_cat==1
replace maeduc_yrs=0 if maeduc_cat==2
replace maeduc_yrs=6 if maeduc_cat==3
replace maeduc_yrs=9 if maeduc_cat==4
replace maeduc_yrs=9 if maeduc_cat==5
replace maeduc_yrs=12 if maeduc_cat==6
replace maeduc_yrs=12 if maeduc_cat==7
replace maeduc_yrs=12 if maeduc_cat==8
replace maeduc_yrs=14 if maeduc_cat==9
replace maeduc_yrs=14 if maeduc_cat==10
replace maeduc_yrs=15 if maeduc_cat==11
replace maeduc_yrs=14 if maeduc_cat==12
replace maeduc_yrs=14 if maeduc_cat==13
replace maeduc_yrs=14 if maeduc_cat==14
replace maeduc_yrs=14 if maeduc_cat==15
replace maeduc_yrs=15 if maeduc_cat==16
replace maeduc_yrs=16 if maeduc_cat==17
replace maeduc_yrs=19 if maeduc_cat==18
replace maeduc_yrs=. if maeduc_cat==19 | maeduc_cat==96 | maeduc_cat==97 | maeduc_cat==98
lab var maeduc_yrs "mother's education in years"

ge faeduc_ISCED=020 if faeduc_cat==1
replace faeduc_ISCED=020 if faeduc_cat==2
replace faeduc_ISCED=100 if faeduc_cat==3
replace faeduc_ISCED=200 if faeduc_cat==4
replace faeduc_ISCED=200 if faeduc_cat==5
replace faeduc_ISCED=340 if faeduc_cat==6
replace faeduc_ISCED=350 if faeduc_cat==7
replace faeduc_ISCED=350 if faeduc_cat==8
replace faeduc_ISCED=300 if faeduc_cat==9
replace faeduc_ISCED=500 if faeduc_cat==10
replace faeduc_ISCED=500 if faeduc_cat==11
replace faeduc_ISCED=500 if faeduc_cat==12
replace faeduc_ISCED=500 if faeduc_cat==13
replace faeduc_ISCED=500 if faeduc_cat==14
replace faeduc_ISCED=500 if faeduc_cat==15
replace faeduc_ISCED=500 if faeduc_cat==16
replace faeduc_ISCED=600 if faeduc_cat==17
replace faeduc_ISCED=700 if faeduc_cat==18
replace faeduc_ISCED=. if faeduc_cat==19
lab var faeduc_ISCED "father highest education in years"

ge maeduc_ISCED=020 if maeduc_cat==1
replace maeduc_ISCED=020 if maeduc_cat==2
replace maeduc_ISCED=100 if maeduc_cat==3
replace maeduc_ISCED=200 if maeduc_cat==4
replace maeduc_ISCED=200 if maeduc_cat==5
replace maeduc_ISCED=340 if maeduc_cat==6
replace maeduc_ISCED=350 if maeduc_cat==7
replace maeduc_ISCED=350 if maeduc_cat==8
replace maeduc_ISCED=300 if maeduc_cat==9
replace maeduc_ISCED=500 if maeduc_cat==10
replace maeduc_ISCED=500 if maeduc_cat==11
replace maeduc_ISCED=500 if maeduc_cat==12
replace maeduc_ISCED=500 if maeduc_cat==13
replace maeduc_ISCED=500 if maeduc_cat==14
replace maeduc_ISCED=500 if maeduc_cat==15
replace maeduc_ISCED=500 if maeduc_cat==16
replace maeduc_ISCED=600 if maeduc_cat==17
replace maeduc_ISCED=700 if maeduc_cat==18
replace maeduc_ISCED=. if maeduc_cat==19
lab var maeduc_ISCED "mother highest education in years"


** 14. Homoginising sibling (by Manting Chen, 2017/4/3)**
//cutoff
ge nbro_flag=99
lab var nbro_flag "cutoff of number of brothers"
ge nsis_flag=99
lab var nsis_flag "cutoff of number of sisters"
ge nsibs_flag=99
lab var nsibs_flag "cutoff of total number of siblings"

lab def nsib_flag 99 "no cutoff"
lab val nbro_flag nsis_flag nsibs_flag nsib_flag

//recode missing
replace nbro=. if nbro==99 | nbro==102 | nbro==194 | nbro==196
replace nsis=. if nsis==99 | nsis==194 | nsis==196
replace nsibs=. if nsibs==101 | nsibs==198 | nsibs==295 | nsibs==298 | nsibs==388 | nsibs==392 

** 15. Tab Education and Sibling Variables **
tab1 sex age birthyr
tab1 educ_cat educ_yrs faeduc_cat faeduc_yrs maeduc_cat maeduc_yrs faeduc_flag 
tab1 nbro nsis nsibs nbro_flag nsis_flag nsibs_flag


** 16. Save the Data File **

saveold /*insert you work directory here*/, replace
